package com.athena.view
{
	import com.adobe.serialization.json.JSON;
	import com.athena.controller.Bank;
	import com.athena.controller.ExchangeGift;
	import com.athena.controller.FeedBack;
	import com.athena.controller.Mail;
	import com.athena.controller.Rooms;
	import com.athena.Objects.PlayerObject;
	import com.athena.utils.Delegate;
	import com.athena.utils.Util;
	import com.greensock.loading.SWFLoader;
	import com.junkbyte.console.Cc;
	import fl.containers.ScrollPane;
	import fl.controls.CheckBox;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author chau
	 */
	public class DashBoard extends EventDispatcher
	{
		private var _movie : MovieClip;
		public static var CHOINGAY : String = "choingay";
		private var _rooms : Rooms;
		
		private var _mailController : Mail;
		private var _bankController : Bank;
		public function DashBoard(_mc : MovieClip, strRoom : String) {
			_movie = _mc;
			_movie.tabEnabled = false;
			_movie.tabChildren = false;
			_rooms = new Rooms(_movie, strRoom);
			setUp();
		}
		private function setUp() : void {
			try {
				//display info user
				
				displayInfo();
				//chat box
				_movie.addChild(Main.getInstance().chatRoom.movie);
				
				_movie.join_txt.restrict = "0-9";
				
				var chk : CheckBox = new CheckBox();
				if (Main.getInstance().sysSetting) {
					chk.selected = !Main.getInstance().sysSetting.invite;
				}
				chk.label = "Từ chối lời mời";
				chk.x = 573; chk.y = 11;
				chk.width = 150;
				var _tf : TextFormat = new TextFormat();
				_tf.color = 0x999999;
				_tf.size = 12;
				_tf.font = "Tahoma";
				chk.setStyle("textFormat", _tf);
				chk.addEventListener(Event.CHANGE, setInvite);
				_movie.lobby_mc.addChild(chk);
				addEvent();
				
			}catch (err:Error) {
				Cc.error(err + "setup dashboard");
			}
		}
		
		private function setInvite(e : Event) : void {
			Main.getInstance().sysSetting.invite = !(e.currentTarget as CheckBox).selected;
			sendChangeSetting();
		}
		
		public function sendChangeSetting() : void {
			var obj : Object = { };
			obj.evt = "US";
			obj.bm = Main.getInstance().sysSetting.backgroundSound;
			obj.s = Main.getInstance().sysSetting.sound;
			obj.i = Main.getInstance().sysSetting.invite;
			Main.getInstance().socket.sendService(JSON.encode(obj));
		}
		public function updateMark(mark : int) : void {
			Main.getInstance().currPlayer.mark += mark;
			_movie.cash_mc.cash_txt.text = Util.numberFormat(Main.getInstance().currPlayer.mark, 0);
			
			_movie.cash_mc.x = _movie.vip_mc.x - _movie.cash_mc.width - 10;
			_movie.name_mc.x = _movie.cash_mc.x - _movie.name_mc.width - 10;
		}
		public function addEvent() : void {
			_movie.taoban_btn.addEventListener(MouseEvent.CLICK, createTable);
			_movie.join_btn.addEventListener(MouseEvent.CLICK, sendJoin);
			_movie.mail_btn.addEventListener(MouseEvent.CLICK, openMail);
			_movie.bank_btn.addEventListener(MouseEvent.CLICK, openBank);
			_movie.gift_btn.addEventListener(MouseEvent.CLICK, openGift);
			_movie.feed_btn.addEventListener(MouseEvent.CLICK, openFeed);
		}
		
		public function remove() : void {
			removeEvent();
			if (_movie.parent) _movie.parent.removeChild(_movie);
		}
		private function removeEvent() : void {
			_movie.taoban_btn.removeEventListener(MouseEvent.CLICK, createTable);
			_movie.join_btn.removeEventListener(MouseEvent.CLICK, sendJoin);
			_movie.mail_btn.removeEventListener(MouseEvent.CLICK, openMail);
			_movie.bank_btn.removeEventListener(MouseEvent.CLICK, openBank);
			_movie.gift_btn.removeEventListener(MouseEvent.CLICK, openGift);
			_movie.feed_btn.removeEventListener(MouseEvent.CLICK, openFeed);
		}
		
		private function openMail(e : MouseEvent) : void {
			_mailController = new Mail();
			Main.getInstance().addChild(_mailController);
		}
		private function openBank(e : MouseEvent) : void {
			_bankController = new Bank();
			Main.getInstance().addChild(_bankController);
		}
		
		private function openGift(e : MouseEvent) : void {
			var _giftController : ExchangeGift = new ExchangeGift();
			Main.getInstance().addChild(_giftController);
		}
		
		private function openFeed(e : MouseEvent) : void {
			var _feedController : FeedBack = new FeedBack();
			Main.getInstance().addChild(_feedController);
		}
		
		private function displayInfo() : void {
			_movie.name_mc.name_txt.autoSize = TextFieldAutoSize.LEFT;
			_movie.cash_mc.cash_txt.autoSize = TextFieldAutoSize.LEFT;
			_movie.name_mc.name_txt.text = Main.getInstance().currPlayer.displayName;
			_movie.cash_mc.cash_txt.text = Util.numberFormat(Main.getInstance().currPlayer.mark, 0);
			
			_movie.cash_mc.x = _movie.vip_mc.x - _movie.cash_mc.width - 10;
			_movie.name_mc.x = _movie.cash_mc.x - _movie.name_mc.width - 10;
			
			_movie.vip_mc.gotoAndStop(Main.getInstance().currPlayer.vip + 1);
		}
		
		private function sendJoin(e : MouseEvent) : void {
			if (_movie.join_txt.text == "") return;
			var _id : int = parseInt(_movie.join_txt.text);
			Main.getInstance().socket.sendJoinTableRequest(_id);
		}
		private function createTable(e : MouseEvent) : void {
			var sts : settingTable = new settingTable();
			_movie.addChild(sts);
		}
		public function removeRoom() : void {
			while (_movie.lobby_mc.content_mc.numChildren > 0) {
				var _mc : MovieClip = MovieClip(_movie.lobby_mc.content_mc.getChildAt(0));
				_movie.lobby_mc.content_mc.removeChild(_mc);
				_mc.removeEventListener(MouseEvent.CLICK, sendClickRoom);
				_mc = null;
			}
		}
		public function addListTable(_arr : Array) : void {
			var cls : Class = (Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("com.athena.ui.RoomLobby") as Class;
			var j : int = 0;
			for each(var obj : Object in _arr) {
				var room : MovieClip = MovieClip(new cls());
				room.id = obj.Id;
				var x : int = j % 4; var y : int = Math.floor(j / 4);
				room.x = 176.3 * x; room.y = 101 * y;
				room.gotoAndStop("room" + Main.getInstance().config.maxPLayer);
				room.content_mc.mark_txt.mouseEnabled = false;
				room.content_mc.mark_txt.text = Util.numberFormat(obj.M , 0) + Main.getInstance().gameObject.UnitMark;
				room.content_mc.id_txt.text = obj.Id;
				//room.content_mc.mouseChildren = false;
				var _arrayPl : Array = obj.ArrN as Array;
				
				for (var i : int = 0 ; i < _arrayPl.length; i++ ) {
					var txt : TextField = TextField(room.content_mc.getChildByName("player" + i + "_txt"));
					if (txt) {
						txt.text = _arrayPl[i];
						txt.mouseEnabled = false;
					}
				}
				_movie.lobby_mc.content_mc.addChild(room);				
				room.addEventListener(MouseEvent.CLICK, sendClickRoom);
				j++;
			}
			//add scroll
				var scr : ScrollPane = ScrollPane(_movie.lobby_mc.getChildByName("scrollerLobby"));
				if (scr == null) {
					scr = new ScrollPane();
					scr.name = "scrollerLobby";
					scr.setSize(720, 290);
					scr.x = 10; scr.y = 40;
					var mc:MovieClip = new MovieClip();
					scr.setStyle("upSkin", mc);
					scr.source = _movie.lobby_mc.content_mc;
					_movie.lobby_mc.addChild(scr);
					
					scr.setStyle("scrollBarWidth", 10);
					scr.setStyle("scrollArrowHeight", 10);
					//scr.setStyle("trackUpSkin", new ScrollTrack_LobbySkin());
					//scr.setStyle("trackOverSkin", new ScrollTrack_LobbySkin()); 
					//scr.setStyle("trackDownSkin", new ScrollTrack_LobbySkin()); 
					//scr.setStyle("thumbUpSkin", new ScrollThumb_LobbyUpSkin());
					//scr.setStyle("thumbOverSkin", new ScrollThumb_LobbyUpSkin());
					//scr.setStyle("thumbDownSkin", new ScrollThumb_LobbyUpSkin()); 
					//scr.setStyle("upArrowUpSkin", new ((Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("ScrollArrowUpLobby_downSkin") as Class));
					//scr.setStyle("upArrowOverSkin", new ((Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("ScrollArrowUpLobby_downSkin") as Class));
					//scr.setStyle("upArrowDownSkin", new ((Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("ScrollArrowUpLobby_downSkin") as Class)); 
					//scr.setStyle("downArrowUpSkin", new ((Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("ScrollArrowDownLobby_downSkin") as Class));
					//scr.setStyle("downArrowOverSkin", new ((Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("ScrollArrowDownLobby_downSkin") as Class));
					//scr.setStyle("downArrowDownSkin", new ((Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("ScrollArrowDownLobby_downSkin") as Class));
					
				}else {
					scr.update();
				}
		}
		private function sendClickRoom(e : MouseEvent) : void {
			Main.getInstance().socket.sendJoinTableRequest((e.currentTarget as MovieClip).id);
		}
		private function sendPlayNow(e : MouseEvent) : void {
			dispatchEvent(new Event(CHOINGAY));
		}
		public function get movie():MovieClip 
		{
			return _movie;
		}
		
		public function get rooms():Rooms 
		{
			return _rooms;
		}
		
		public function get mailController():Mail 
		{
			return _mailController;
		}
		
		public function get bankController():Bank 
		{
			return _bankController;
		}
		
	
	}

}